import lyRequest from '@/service'
import { BatchBtns, SingleBtns } from '@/global/constant'

// 获取登录验证码
export function getVerifyCode() {
  return lyRequest.get({
    url: '/getVerifyCode'
  })
}

/**
 * 登录接口
 * @param info
 * info携带的userName、password、code参数均为string类型
 */
export function reqAccountLogin(info: any) {
  return lyRequest.post({
    url: '/login',
    data: info,
    isShowMsg: true
  })
}

/**
 * 通过id请求用户的基本信息
 * @param userId: number
 */
export function getUserInfoById(userId: number) {
  return lyRequest.post({
    url: `/getUserInfo/${userId}`
  })
}

/**
 * 通过id请求用户菜单列表
 * @param userId: number
 */
export function getMenuListById(userId: number) {
  return lyRequest.get({
    url: `/getMenuListById`,
    params: { userId }
  })
}

/**
 * 通过id退出登录
 * @param userId: number
 */
export function reqAccountLogoutById(userId: number) {
  return lyRequest.post({
    url: `/logout/${userId}`,
    isShowMsg: true
  })
}

/**
 * 修改密码
 * @param body: {id: number, newPassword: string, oldPassword: string}
 */
interface IPassword {
  id: number
  newPassword: string
  oldPassword: string
}

// 更新密码
export function reqUpdatePassword(info: IPassword) {
  return lyRequest.post({
    url: '/user/updatePassword',
    data: info,
    isShowMsg: true
  })
}

// 二次认证
export function reqSecCertification(
  password: string,
  pageName: string, // 页面名称
  btnType: string, // 按钮类型
  skuType: number = -1 // sku页面区分规格还是规格值
) {
  let safeType
  // 用户管理页的批量删除
  if (pageName === 'user' && btnType === BatchBtns.Delete) {
    safeType = 'batch-user-delete'
  }
  // 用户管理页的单删除
  if (pageName === 'user' && btnType === SingleBtns.Delete) {
    safeType = 'user-delete'
  }
  // 用户管理页的重置密码
  if (
    (pageName === 'user' || pageName === 'merchant' || pageName === 'admin') &&
    btnType === SingleBtns.ResetPwd
  ) {
    safeType = 'reset-password'
  }
  // 角色管理页的单删除
  if (pageName === 'role' && btnType === SingleBtns.Delete) {
    safeType = 'role-delete'
  }
  // 角色管理页的批量删除
  if (pageName === 'role' && btnType === BatchBtns.Delete) {
    safeType = 'batch-role-delete'
  }
  // 经营页面的单删除
  if (pageName === 'merchantType' && btnType === SingleBtns.Delete) {
    safeType = 'manage-delete'
  }
  // 经营页面的批量删除
  if (pageName === 'merchantType' && btnType === BatchBtns.Delete) {
    safeType = 'batch-manage-delete'
  }
  // 商户页面的单删除
  if (pageName === 'merchant' && btnType === SingleBtns.Delete) {
    safeType = 'merchant-delete'
  }
  // 商户页面的批量删除
  if (pageName === 'merchant' && btnType === BatchBtns.Delete) {
    safeType = 'batch-merchant-delete'
  }
  // 商户申请的单删除
  if (pageName === 'merchantApply' && btnType === SingleBtns.Delete) {
    safeType = 'merchant-apply-delete'
  }
  // 商户申请的批量删除
  if (pageName === 'merchantApply' && btnType === BatchBtns.Delete) {
    safeType = 'batch-merchant-apply-delete'
  }
  // 商户申请升级的单删除
  if (pageName === 'merchantUpgrade' && btnType === SingleBtns.Delete) {
    safeType = 'merchant-upgrade-delete'
  }
  // 商户申请升级的批量删除
  if (pageName === 'merchantUpgrade' && btnType === BatchBtns.Delete) {
    safeType = 'batch-merchant-upgrade-delete'
  }
  // 管理员页面的单删除
  if (pageName === 'admin' && btnType === SingleBtns.Delete) {
    safeType = 'admin-delete'
  }
  // 管理员页面的批量删除
  if (pageName === 'admin' && btnType === BatchBtns.Delete) {
    safeType = 'batch-admin-delete'
  }
  // 菜单页面的删除
  if (pageName === 'menu' && btnType === SingleBtns.Delete) {
    safeType = 'menu-delete'
  }
  // 商品品牌页面的删除
  if (pageName === 'goodBrand' && btnType === SingleBtns.Delete) {
    safeType = 'brand-delete'
  }
  // 商品sku页面的删除
  if (pageName === 'goodSku' && btnType === SingleBtns.Delete) {
    skuType === 1 && (safeType = 'specification-value-delete')
    skuType === 0 && (safeType = 'specification-delete')
  }
  // 商品分类页面的删除
  if (pageName === 'goodClassify' && btnType === SingleBtns.Delete) {
    safeType = 'classify-delete'
  }
  return lyRequest.post({
    url: '/user/openSafe',
    data: { password, safeType },
    isShowMsg: true
  })
}
